﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using System.Collections;

namespace StudentManage.Web.Admin.Management.PersonalManage
{
    public partial class UnAuditPersonalList : SecurityPage
    {
        int i = 0;
        public string GetXN()
        {
            if (i == 0)
                i = (this.AspNetPager1.PageSize) * (this.AspNetPager1.CurrentPageIndex - 1);

            i += 1;
            return i.ToString();
        }

        private void RepPostBing()
        {
            int Pid = Convert.ToInt32(Request.QueryString["Pid"].ToString());

            var eps = DynamicLinqExpressions.True<Apply>();
            eps = eps.And(p => p.Projectid == Pid);
            eps = eps.And(p => p.IsDel == false);
            eps = eps.And(p => p.IsAudit == false);
            eps = eps.And(p => p.UserName.Contains(this.txt_name.Text.Trim()));//模糊查询

            int RowCount = LinqUtility.LinqContext.Apply.Where(eps).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            var q = from A in LinqUtility.LinqContext.Apply
                    join B in LinqUtility.LinqContext.UserList on A.Userid equals B.id
                    join C in LinqUtility.LinqContext.xy_xsxx on B.XH equals C.XH 
                    where A.Projectid == Pid && A.IsDel == false && A.IsAudit == false && A.UserName.Contains(this.txt_name.Text.Trim())
                    select new { A.id, A.AddTime, A.UserName, B.XH, B.Tel, C.XZB,A.IsDel,A.IsAudit};

            this.RepPostList.DataSource = q.OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.RepPostList.DataBind();
        }

        private void DropBing()
        {
            List<xy_bj> list = xy_bj.All().ToList();
            list.Add(new xy_bj() {BJMC="全部",BJDM="0"});
            list = list.OrderBy(p => p.BJDM).ToList();
            this.Drop_bj.DataSource = list;
            this.Drop_bj.DataTextField = "BJMC";
            this.Drop_bj.DataValueField = "BJDM";
            this.Drop_bj.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DropBing();
            if (Request.QueryString["Pid"] != null)
                RepPostBing();
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
            int Applyid = Convert.ToInt32(this.HidShowId.Value);
            Apply md = Apply.Where("id=@0", Applyid).First();
            
            //判断是否有已经审批通过的岗位
            if (Apply.Where("Userid=@0 and IsAudit=@1 and IsDel=@2", md.Userid, true, false).Count() > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {alert('该学生已经存在审批通过的岗位！');});</script>");
                return;
            }

            md.IsAudit = true;
            md.Auditid = Umd.id;
            md.AuditTime = DateTime.Now;
            Apply.Update(md, "id=@0", md.id);
            
            this.RepPostBing();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {alert('审批成功！');});</script>");
        }

        protected void RepPostList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int Applyid = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                Project.Delete("id=@0", Applyid);
                this.RepPostBing();
            }
            else
            {
                Apply md = Apply.Where("id=@0", Applyid).First();
                UserList user = UserList.Where("id=@0", md.Userid).First();
                xy_xsxx xs=xy_xsxx.Where("XH=@0",user.XH).First();

                this.HidShowId.Value = Applyid.ToString();

                this.lab_UserClass.Text = xs.XZB;
                this.lab_UserNum.Text = user.XH;
                this.lab_UserName.Text = md.UserName.ToString();
                this.lab_UserSex.Text = xs.XB;
                this.lab_UserTel.Text = user.Tel;
                this.lab_UserEmail.Text = user.Email;
                this.lab_UserSpeciality.Text = user.Specialty;
                this.lab_UserExperience.Text = user.Experience;
                this.lab_UserHobby.Text = user.Hobby;
                this.lab_UserReason.Text = md.Reason;
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {Showdlg() });</script>");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepPostBing();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepPostBing();
        }

    }
}